package test.yzx.pproject.view;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.FloatRange;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class CircularContractionView extends View{

    public CircularContractionView(Context context) {
        this(context, null);
    }
    public CircularContractionView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CircularContractionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }



    /**
     *
     * */
    public void setData(Bitmap bmp, Point endPoi, int radius, int endColor, int duration){
        this.bitmap  = bmp;
        this.endPoi = endPoi;
        this.endRadius = radius;
        this.endColor = endColor;
        this.duration = duration;
    }
    /**
     *
     * */
    public void start(Runnable onEnd){
        this.onEndCallback = onEnd;
        if(anim !=null){
            anim.removeAllUpdateListeners();
            anim.removeAllListeners();
            anim.cancel();
        }
        anim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
        anim.setInterpolator(new LinearInterpolator());
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                progress = (float) animation.getAnimatedValue();
                invalidate();
            }});
        anim.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                isAnimEnding = true;
                invalidate();
                if(onEndCallback!=null)
                    onEndCallback.run();
            }});
        none = false;
        isAnimEnding = false;
        anim.start();
    }
    /**
     *
     * */
    public void reset(){
        if(anim !=null){
            anim.removeAllUpdateListeners();
            anim.removeAllListeners();
            anim.cancel();
        }
        progress = 0f;
        none = true;
        isAnimEnding = false;
        invalidate();
    }




    private int endColor;
    private int endRadius;
    private Point endPoi;
    private int duration;
    private Bitmap bitmap;
    private Runnable onEndCallback;
    private ValueAnimator anim;
    private @FloatRange(from = 0f, to = 1f) float progress = 0f;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private boolean none = true;
    private Path clipPath = new Path();
    private boolean isAnimEnding = false;


    @Override
    protected void onDraw(Canvas canvas) {
        if(bitmap==null || none){
            super.onDraw(canvas);
            return ;
        }

        int fullDiagonal = (int) Math.sqrt(getWidth()*getWidth() + getHeight() *getHeight());
        clipPath.reset();
        clipPath.addCircle(endPoi.x, endPoi.y, fullDiagonal * (1f - progress) + endRadius, Path.Direction.CCW);
        canvas.clipPath(clipPath);

        canvas.drawBitmap(bitmap, 0, 0, null);

        if(isAnimEnding){
            paint.setColor(endColor);
            canvas.drawColor(endColor);
        }
    }

}
